define(['app', 'js/services/MessageBox', 'comm/pager/PagerControl',
    'js/services/MuseService'], function (app) {

    var deps = ['$scope', '$state', '$modal', 'MessageBox', 'MuseService'];

    function controller ($scope, $state, $modal, MessageBox, MuseService) {
        //初始化查询对象
        app.regsiterCacheData($scope, $state);
        $scope.queryObj = $scope.queryObj || {};
        $scope.compCorpusTypeList = {};

        $scope.pagerObj = {
            pageSize: 10,
            currentPage: 1
        };

        //获取列表
        $scope.list = function () {
            angular.extend($scope.queryObj, $scope.pagerObj);
            MuseService.post('admin/compCorpusType/list.json', $scope.queryObj).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.compCorpusTypeList = data.data;
                        //分页器
                        $scope.pagerObj = PageCtrl().page($scope.pagerObj, data.totalPage, $scope.pagerObj.currentPage);
                    } else {
                        MessageBox.notify({msg: data.message, displayTime: 1500});
                    }
                });
        };

        $scope.list();

        //编辑
        $scope.edit = function (obj) {
            obj.oldName = obj.name;
            obj.isEdit = true;
            var _focus = setTimeout(function () {
                document.querySelector('#compCorpusType_' + obj.id).focus();
            }, 0);
            //TODO 直接name修改，然后调用保存
        };

        //选中
        //TODO 1.添加选中背景
        $scope.select = function (obj, e) {
            angular.element(document.querySelectorAll('tr.selected')).removeClass('selected');
            angular.element(e.currentTarget).addClass('selected');
            $state.go("compCorpusTypeList.compCorpusList", {compCorpusTypeId: obj.id});
        }

        //保存
        $scope.save = function (obj) {
            if (obj.oldName == obj.name) {
                return false;
            }
            if (obj.name == '') {
                MessageBox.error('请输入分类名');
                return false;
            }
            if (obj.name.length < 2 || obj.name.length > 20) {
                MessageBox.error('分类名长度应在2～20个字符之间');
                return false;
            }
            //check name

            MuseService.postObj('admin/compCorpusType/save.json', obj).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        MessageBox.notify({msg: '保存成功！', displayTime: 1000});
                        $scope.list();
                    } else {
                        MessageBox.error('保存失败：' + data.message);
                    }
                }
            );
        };

        ///删除
        $scope.delete = function (id) {
            var dialog = MessageBox.confirm('确认要删除该信息吗？');
            dialog.result.then(function () {
                MuseService.post('admin/compCorpusType/delete.json', {id: id}).success(
                    function (data) {
                        if (data.state == 0) {
                            MessageBox.notify({msg: '删除成功！', displayTime: 1000});
                            $scope.list();
                        } else {
                            MessageBox.error('删除异常：' + data.message);
                        }
                    });
            });
        };

        $scope.pageChanged = function () {
            PageCtrl().check($scope.pagerObj);
            $scope.list();
        }

    }

    controller.$inject = deps;
    app.lazy.controller('CompCorpusTypeListController', controller);
});
